﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using YArchitech.Controls;
using YArchitech.LIB;
using HYElectricSystemDiagram.DataStruct;

namespace HYElectricSystemDiagram.FormSet
{
	public partial class FormNameSet : HYBaseForm
	{
		public FormNameSet()
		{
			this.InitializeComponent();
			this.btnAdd.Image = Resource.GetImage("AddNew16");
			this.btnModify.Image = Resource.GetImage("modify16");
			this.btnDelete.Image = Resource.GetImage("delete16");
			foreach (KeyValuePair<string, string> keyValuePair in GetXmlData.GetNamSet())
			{
				int index = this.dgvRoomKeyInfo.Rows.Add();
				this.dgvRoomKeyInfo.Rows[index].Cells[0].Value = keyValuePair.Value;
				this.dgvRoomKeyInfo.Rows[index].Cells[1].Value = keyValuePair.Key;
				this.lstName.Add(keyValuePair.Value);
				this.lstTypeName.Add(keyValuePair.Key);
			}
		}

		private void DataBind()
		{
			this.dgvRoomKeyInfo.Rows.Clear();
			for (int i = 0; i < this.lstName.Count; i++)
			{
				int index = this.dgvRoomKeyInfo.Rows.Add();
				this.dgvRoomKeyInfo.Rows[index].Cells[0].Value = this.lstName[i];
				this.dgvRoomKeyInfo.Rows[index].Cells[1].Value = this.lstTypeName[i];
			}
		}

		private void dgvRoomKeyInfo_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
		{
			DataGridView dataGridView = (DataGridView)sender;
			if (e.RowIndex < 0 || e.RowIndex >= dataGridView.Rows.Count)
			{
				e.Cancel = true;
			}
		}

		private void dgvRoomKeyInfo_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			if (e.RowIndex < 0 || e.RowIndex >= this.dgvRoomKeyInfo.Rows.Count)
			{
				return;
			}
			if (this.dgvRoomKeyInfo.Rows[e.RowIndex].Cells["KeyName"].Value == null)
			{
				return;
			}
			if (string.IsNullOrEmpty(this.dgvRoomKeyInfo.Rows[e.RowIndex].Cells["KeyName"].Value.ToString()))
			{
				this.dgvRoomKeyInfo.Rows[e.RowIndex].Cells["KeyName"].Selected = true;
				return;
			}
			string text = this.dgvRoomKeyInfo.Rows[e.RowIndex].Cells["KeyName"].Value.ToString();
			if (text == null)
			{
				return;
			}
			int num = 0;
			using (List<string>.Enumerator enumerator = this.lstName.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current == text)
					{
						this.lstName[num] = Convert.ToString(this.dgvRoomKeyInfo.Rows[e.RowIndex].Cells["KeyName"].Value);
						this.lstTypeName[num] = Convert.ToString(this.dgvRoomKeyInfo.Rows[e.RowIndex].Cells["TypeName"].Value);
					}
					num++;
				}
			}
		}

		private void dgvRoomKeyInfo_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
		{
			if (e.RowIndex >= this.dgvRoomKeyInfo.Rows.Count)
			{
				return;
			}
			if (this.dgvRoomKeyInfo.Rows[e.RowIndex].Cells["TypeName"].ColumnIndex == e.ColumnIndex || e.RowIndex < 0)
			{
				return;
			}
			if (this.dgvRoomKeyInfo.Columns[e.ColumnIndex].Name == "KeyName" && e.RowIndex >= 0)
			{
				if (e.FormattedValue == null || string.IsNullOrEmpty(e.FormattedValue.ToString()))
				{
					MessageBox.Show("不能为空。", "盈建科提示");
					e.Cancel = true;
				}
				foreach (object obj in ((IEnumerable)this.dgvRoomKeyInfo.Rows))
				{
					DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
					string[] array = e.FormattedValue.ToString().Split(new char[]
					{
						',',
						'，'
					});
					if (dataGridViewRow.Index != e.RowIndex && dataGridViewRow.Cells[e.ColumnIndex].Value != null)
					{
						foreach (string b in dataGridViewRow.Cells[e.ColumnIndex].Value.ToString().Split(new char[]
						{
							',',
							'，'
						}))
						{
							string[] array3 = array;
							for (int j = 0; j < array3.Length; j++)
							{
								if (array3[j] == b)
								{
									MessageBox.Show("不能重复。", "盈建科提示");
									e.Cancel = true;
									return;
								}
							}
						}
					}
				}
			}
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			if (this.dgvRoomKeyInfo.SelectedRows.Count == 0)
			{
				return;
			}
			this.dgvRoomKeyInfo.Columns["KeyName"].ReadOnly = false;
			foreach (object obj in ((IEnumerable)this.dgvRoomKeyInfo.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				dataGridViewRow.Cells["TypeName"].Value.ToString();
				if (dataGridViewRow.Cells["KeyName"].Value.ToString().Equals("配电盘名称"))
				{
					MessageBox.Show("配电盘名称不能重复。", "盈建科提示");
					return;
				}
			}
			int num = this.dgvRoomKeyInfo.Rows.Add();
			this.dgvRoomKeyInfo.Rows[num].Cells[0].Value = "普通风机控制箱";
			this.dgvRoomKeyInfo.Rows[num].Cells[1].Value = "配电盘名称";
			this.dgvRoomKeyInfo.ClearSelection();
			this.dgvRoomKeyInfo.Rows[num].Selected = true;
			this.dgvRoomKeyInfo.Rows[num].Visible = true;
			this.dgvRoomKeyInfo.FirstDisplayedScrollingRowIndex = num;
			this.dgvRoomKeyInfo.CurrentCell = this.dgvRoomKeyInfo.Rows[num].Cells["TypeName"];
			this.dgvRoomKeyInfo.BeginEdit(true);
			this.cbName.Visible = true;
			System.Drawing.Rectangle cellDisplayRectangle = this.dgvRoomKeyInfo.GetCellDisplayRectangle(0, this.dgvRoomKeyInfo.CurrentCell.RowIndex, false);
			this.cbName.Top = cellDisplayRectangle.Top + cellDisplayRectangle.Height * 2;
			this.cbName.Left = cellDisplayRectangle.Left + 10;
			this.cbName.Width = cellDisplayRectangle.Width;
			this.cbName.Height = cellDisplayRectangle.Height;
			this.cbName.SelectedIndex = 0;
			this.Change(num);
		}

		private void btnModify_Click(object sender, EventArgs e)
		{
			if (this.dgvRoomKeyInfo.SelectedRows.Count > 0)
			{
				this.dgvRoomKeyInfo.Columns["KeyName"].ReadOnly = false;
				this.dgvRoomKeyInfo.Columns["TypeName"].ReadOnly = false;
				this.dgvRoomKeyInfo.CurrentCell = this.dgvRoomKeyInfo.SelectedRows[0].Cells["KeyName"];
				this.dgvRoomKeyInfo.BeginEdit(true);
			}
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			if (this.dgvRoomKeyInfo.SelectedRows.Count > 0 && MessageBox.Show("确定要删除此信息？", "盈建科提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
			{
				int index = this.dgvRoomKeyInfo.SelectedRows[0].Index;
				string item = this.dgvRoomKeyInfo.SelectedRows[0].Cells["KeyName"].Value.ToString();
				string item2 = this.dgvRoomKeyInfo.SelectedRows[0].Cells["TypeName"].Value.ToString();
				if (!this.lstName.Contains(item) || !this.lstTypeName.Contains(item2))
				{
					this.dgvRoomKeyInfo.Rows.RemoveAt(this.dgvRoomKeyInfo.Rows.Count - 1);
					this.cbName.Visible = false;
					return;
				}
				this.lstName.Remove(item);
				this.lstTypeName.Remove(item2);
				this.DataBind();
				if (this.dgvRoomKeyInfo.Rows.Count > 0 && this.dgvRoomKeyInfo.Rows.Count != 1)
				{
					if (this.dgvRoomKeyInfo.Rows.Count == index)
					{
						this.dgvRoomKeyInfo.Rows[this.dgvRoomKeyInfo.Rows.Count - 1].Selected = true;
						return;
					}
					this.dgvRoomKeyInfo.Rows[index].Selected = true;
				}
			}
		}

		private void btnCancle_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			this.lstName.Clear();
			this.lstTypeName.Clear();
			foreach (object obj in ((IEnumerable)this.dgvRoomKeyInfo.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				string item = dataGridViewRow.Cells["TypeName"].Value.ToString();
				string item2 = dataGridViewRow.Cells["KeyName"].Value.ToString();
				this.lstName.Add(item);
				this.lstTypeName.Add(item2);
			}
			GetXmlData.WriteNameSet(this.lstName, this.lstTypeName);
		}

		private void btnAdd_MouseEnter(object sender, EventArgs e)
		{
			this.tipBtn.SetToolTip(this.btnAdd, "添加");
		}

		private void btnModify_MouseEnter(object sender, EventArgs e)
		{
			this.tipBtn.SetToolTip(this.btnModify, "修改");
		}

		private void btnDelete_MouseEnter(object sender, EventArgs e)
		{
			this.tipBtn.SetToolTip(this.btnDelete, "删除");
		}

		private void dgvRoomKeyInfo_CurrentCellChanged(object sender, EventArgs e)
		{
			if (this.dgvRoomKeyInfo.CurrentCell == null)
			{
				return;
			}
			if (this.dgvRoomKeyInfo.CurrentCell.ColumnIndex != 0)
			{
				this.cbName.Visible = false;
			}
		}

		private void Change(int index)
		{
			string text = this.cbName.Text;
			this.dgvRoomKeyInfo.Rows[index].Cells[0].Value = text;
		}

		private void cbName_TextChanged(object sender, EventArgs e)
		{
			if (this.cbName.Text == "")
			{
				return;
			}
			this.Change(this.dgvRoomKeyInfo.RowCount - 1);
		}

		private List<string> lstName = new List<string>();

		private List<string> lstTypeName = new List<string>();
	}
}
